﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;
using Utils;

namespace PanasonicWarranty.Business
{
    public partial class pa_MessagePattern
    {
        #region Singletion pattern
        private static Dictionary<string, string> _instance;
        private static readonly object _lock = new object();
        public static Dictionary<string, string> SingletonInstance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
                        {
                            _instance = new Dictionary<string, string>();
                            foreach (var item in context.pa_MessagePatterns.ToList())
                            {
                                _instance.Add(item.PatternCode, item.PatternMessage);
                            }
                        }
                    }
                }
                return _instance;
            }
        }

        public static void ReloadData()
        {
            if (_instance != null)
            {
                lock (_lock)
                {
                    _instance.Clear();
                    using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
                    {
                        foreach (var item in context.pa_MessagePatterns.ToList())
                        {
                            _instance.Add(item.PatternCode, item.PatternMessage);
                        }
                    }
                }
            }
        }
        #endregion Singleton pattern

        public static string GetMessage(int warrantyWaitingId)
        {
            return GetMessage(pa_WarrantyWaiting.GetByID(warrantyWaitingId));
        }

        public static string GetMessage(pa_WarrantyWaiting objwaiting)
        {
            try
            {
                if (objwaiting.WarrantyType == eWarrantyType.DK)
                {
                    return GetMessageDK(objwaiting);
                }
                else if (objwaiting.WarrantyType == eWarrantyType.GH)
                {
                    return GetMessageGH(objwaiting);
                }
                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        private static string GetMessageDK(pa_WarrantyWaiting objwaiting)
        {
            string pattern = SingletonInstance[eMessagePattern.MAUDANGKY];
            using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
            {
                var model = context.pa_Models.FirstOrDefault(p => p.ModelCode == objwaiting.ModelCode);
                if (model != null)
                {
                    return pattern
                    .Replace("(Model)", objwaiting.ModelCode)
                    .Replace("(SoMay)", objwaiting.ProductCode)
                    .Replace("(NgayDangKy)", objwaiting.RegisterDate.ToShortDateString())
                    .Replace("(HanBaoHanh)", model.Description.ToVietnameseWithoutAccent());
                }

            }
            return string.Empty;
        }

        private static string GetMessageGH(pa_WarrantyWaiting objwaiting)
        {
            string pattern = SingletonInstance[eMessagePattern.MAUGIAHAN];
            using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
            {
                var package = context.pa_WarrantyPackages.FirstOrDefault(p => p.WarrantyPackageCode == objwaiting.WarrantyPackageCode);
                if (package != null)
                {
                    DateTime? extentRegisterDate = pa_Warranty.GetWarrantyExtentRegisterDate(objwaiting.ModelCode, objwaiting.ProductCode, objwaiting.RegisterDate);
                    if (extentRegisterDate != null)
                    {
                        return pattern
                                .Replace("(Model)", objwaiting.ModelCode)
                                .Replace("(SoMay)", objwaiting.ProductCode)
                                .Replace("(HanBaoHanh)", package.WarrantyPeriod.ToString())
                                .Replace("(NgayDangKy)", extentRegisterDate.Value.ToShortDateString());
                    }
                }
            }
            return string.Empty;
        }
    }
}
